# 2025年3月10日07:56:32
# 将path1的标签复制到path2, 只保存"0"或"1"开头的行(有，无盔)
import os

def process_file(input_path, output_path):
    with open(input_path, 'r', encoding='utf-8') as infile:
        lines = infile.readlines()
    
    # 过滤出以 "1" 开头的行
    filtered_lines = [line for line in lines if (line.startswith('1') or line.startswith('0'))]
    
    with open(output_path, 'w', encoding='utf-8') as outfile:
        outfile.writelines(filtered_lines)

def process_directory(path1, path2):
    # 确保输出目录存在
    if not os.path.exists(path2):
        os.makedirs(path2)
    
    # 遍历 path1 中的所有文件
    for filename in os.listdir(path1):
        if filename.endswith('.txt'):
            input_file = os.path.join(path1, filename)
            output_file = os.path.join(path2, filename)
            process_file(input_file, output_file)

# 设置路径
#path1 = r"D:\上过的课\2024-2025-1-大四上\毕设\datasets\test\labels"  # 输入目录
#path2 = r"D:\上过的课\2024-2025-1-大四上\毕设\datasets\test\ntlabels"  # 输出目录
path1 = r"D:\上过的课\2024-2025-1-大四上\毕设\datasets\wash1+augmented\labels"  # 输入目录
path2 = r"D:\上过的课\2024-2025-1-大四上\毕设\datasets\wash1+augmented\ntlabels"  # 输出目录

# 处理目录中的所有txt文件
process_directory(path1, path2)